#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>
template <class T>
using func_t = std::function<void(T)>;

template <class T>
class Thread
{
public:
    Thread(const std::string &Threadname, func_t<T> func, T data)
        : _threadname(Threadname), _func(func), _data(data), _isrunning(false), _tid(0)
    {
    }
    static void *ThreadRoutine(void *args)
    {
        Thread* ts = static_cast<Thread*>(args);
        ts->_func(ts->_data);
        return nullptr;
    }
    bool Start()
    {
        int n = pthread_create(&_tid, nullptr, ThreadRoutine, this);
        if (n == 0)
        {
            _isrunning = true;
            return true;
        }
        else
            return false;
    }
    bool Join()
    {
        if (!_isrunning)
            return true;
        int n = pthread_join(_tid, nullptr);
        if (n == 0)
        {
            _isrunning = false;
            return true;
        }
        else
            return false;
    }
    bool isRunning()
    {
        return _isrunning;
    };
    std::string ThreadName()
    {
        return _threadname;
    }

private:
    pthread_t _tid;
    std::string _threadname;
    bool _isrunning;
    func_t<T> _func;
    T _data;
};
class ThreadData
{
public:
    ThreadData(const std::string &name, pthread_mutex_t* mutex)
    :_threadname(name), _mutex(mutex)
    {}
public:
    std::string _threadname;
    pthread_mutex_t* _mutex;
};